package controller;

import model.College;
import service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

@Controller
public class CollegeController{
    private CollegeService collegeService;

    public void setCollegeService(CollegeService collegeService){
        this.collegeService=collegeService;
    }

    @RequestMapping("/getAll_college")
    public ModelAndView getAll() throws Exception{
        ArrayList<College> college=collegeService.getAllColleges();
        ModelAndView modelAndView=new ModelAndView("show_college");
        modelAndView.addObject("Colleges",college);
        return modelAndView;
    }

    @RequestMapping("/delete_college")
    public String delete(HttpServletRequest request, HttpServletResponse response,
                         RedirectAttributes redirectAttributes)throws Exception{
        int collegeId= Integer.parseInt(request.getParameter("collegeId"));
        boolean success=collegeService.deleteCollege(collegeId);

        if(success){
            redirectAttributes.addFlashAttribute("succ", "删除成功！");
            return "redirect:/show_college.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "删除失败！");
            return "redirect:/show_college.jsp";
        }

    }

    @RequestMapping("/get_college")
    public ModelAndView get(HttpServletRequest request) throws Exception{
        int collegeId= Integer.parseInt(request.getParameter("collegeId"));
        String coName= request.getParameter("coName");
        ModelAndView modelAndView=new ModelAndView("show_college1");
        if(collegeId!=0){//按学院编号查询
            ArrayList<College> college=collegeService.getCollege(collegeId);
            modelAndView.addObject("College",college);

        } else if(coName!=null){//按学院名称查询
            ArrayList<College> college=collegeService.getCollege(coName);
            modelAndView.addObject("College",college);

        }
        return modelAndView;
    }

    @RequestMapping("/edit_college")
    public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
        int collegeId= Integer.parseInt(request.getParameter("collegeId"));
        ArrayList<College> college=collegeService.getCollege(collegeId);
        ModelAndView modelAndView=new ModelAndView("show_college");
        modelAndView.addObject("College",college);
        return modelAndView;
    }

    @RequestMapping("/doEdit_college")
    public String doEdit(HttpServletRequest request,HttpServletResponse response,
                         String coName,int number,RedirectAttributes redirectAttributes) throws Exception{
        int collegeId= Integer.parseInt(request.getParameter("collegeId"));
        College college=new College();
        college.setCollegeId(collegeId);
        college.setCoName(coName);
        college.setNumber(number);
        boolean succ=collegeService.editCollege(college);
        if(succ){
            redirectAttributes.addFlashAttribute("succ", "修改成功！");
            return "redirect:/show_college.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "修改失败！");
            return "redirect:/show_college.jsp";
        }

    }

    @RequestMapping("/add_college")
    public ModelAndView add() throws Exception{
        ModelAndView modelAndView=new ModelAndView("show_college");
        return modelAndView;

    }

    @RequestMapping("/doAdd_college")
    public String doAdd(int collegeId,String coName,int number,RedirectAttributes redirectAttributes) throws Exception{
        College college=new College();
        college.setCollegeId(collegeId);
        college.setCoName(coName);
        college.setNumber(number);
        boolean succ=collegeService.addCollege(college);
        if (succ){
            redirectAttributes.addFlashAttribute("succ", "添加成功！");
            return "redirect:/show_college.jsp";
        }else {
            redirectAttributes.addFlashAttribute("succ", "添加失败！");
            return "redirect:/show_college.jsp";
        }
    }
}
